package com.practice.springboot_01_zwj.controller;

import com.practice.springboot_01_zwj.entity.*;
import com.practice.springboot_01_zwj.service.IncomeQuestionService;
import com.practice.springboot_01_zwj.service.ThreeHundredService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@RestController
@RequestMapping("/api/threeHundred")
@RequiredArgsConstructor
public class ThreeHundredController {
    @Autowired
    private ThreeHundredService service;

    @RequestMapping("/selectAll")
    public List<ThreeHundred> selectAll(){
        return service.selectThreeHundredAll();
    }

    @PostMapping("/selectThree")
    public Map<String, Object> selectThree(@RequestBody ThreeHundred request) {
        String cityCode = request.getCityCode();
        String provinceCode = request.getProvinceCode();
        String yearFilter = request.getYearFilter(); // 获取年份筛选参数
        // 验证年份筛选参数
        if (yearFilter != null &&
                !Arrays.asList("2023", "2024", "2025", "all").contains(yearFilter)) {
            throw new IllegalArgumentException("yearFilter 必须是 2023, 2024, 2025 或 all");
        }
        // 参数校验：必须且只能传递一个有效参数
        if ((cityCode == null && provinceCode == null) || (cityCode != null && provinceCode != null)) {
            throw new IllegalArgumentException("必须且只能传递 cityCode 或 provinceCode 中的一个参数");
        }
        if (cityCode != null) {
            return service.selectThreeByCityCode(cityCode,yearFilter);
        } else {
            return service.selectThreeByProvinceCode(provinceCode,yearFilter);
        }
    }
    @PostMapping("/selectThreeHundredAll")
    public PageResult<ThreeHundred> selectThreeHundredAll(@RequestBody ConditionRequest params) {
        // 参数校验
        validateParams(params.getCityCode(), params.getProvinceCode());

        // 处理条件对象
        ThreeHundred hundred = Optional.ofNullable(params.getHundred())
                .orElseGet(ThreeHundred::new);

        // 处理分页参数
        PageRequest pageRequest = processPageRequest(params.getPageRequest());

        // 路由到服务层
        if (StringUtils.hasText(params.getCityCode())) {
            return service.selectCityCodeAll(
                    params.getCityCode(),
                    hundred,
                    pageRequest
            );
        } else {
            return service.selectProvinceCodeAll(
                    params.getProvinceCode(),
                    hundred,
                    pageRequest
            );
        }
    }
    private void validateParams(String cityCode, String provinceCode) {
        boolean hasCity = StringUtils.hasText(cityCode);
        boolean hasProvince = StringUtils.hasText(provinceCode);

        if (hasCity && hasProvince) {
            throw new IllegalArgumentException("城市与省份代码互斥");
        }
        if (!hasCity && !hasProvince) {
            throw new IllegalArgumentException("必须提供区域代码");
        }

        // 扩展校验规则
        if (hasCity && !cityCode.matches("\\d{5}")) {
            throw new IllegalArgumentException("城市代码需为5位数字");
        }
        if (hasProvince && !provinceCode.matches("\\d{5}")) {
            throw new IllegalArgumentException("省份代码需为5位数字");
        }
    }

    private PageRequest processPageRequest(PageRequest request) {
        if (request == null) {
            return new PageRequest(); // 使用默认值
        }

        // 确保页码至少为1
        if (request.getPageNum() == null || request.getPageNum() < 1) {
            request.setPageNum(1);
        }

        // 限制页大小在1-100之间
        if (request.getPageSize() == null || request.getPageSize() < 1) {
            request.setPageSize(20); // 默认20条
        } else if (request.getPageSize() > 100) {
            request.setPageSize(100); // 最大100条
        }

        return request;
    }

}

